#include <PA9.h>
#include "rotatingbackground.h"

namespace game{
    RotatingBackground::RotatingBackground(bool screen, u8 bg_select, void* tiles, u32 tilesize, void* map, void* palette, u32 width, bool wraparound){
        RotatingBackground::screen = screen;
        RotatingBackground::bg_select = bg_select;
        RotatingBackground::bg_tiles = tiles;
        RotatingBackground::bg_tilesize = tilesize;
        RotatingBackground::bg_map = map;
        RotatingBackground::bg_palette = palette;
        
        RotatingBackground::bg_size = PA_GetPAGfxRotBgSize(width);
        RotatingBackground::wraparound = wraparound;
        
        RotatingBackground::loaded = false;
        RotatingBackground::dual = false;
    }
    
    void RotatingBackground::loadbackground(bool screen){
        PA_LoadNormalBgPal(screen, bg_palette);
        PA_DeleteBg(screen, bg_select);
        PA_LoadBgTilesEx(screen, bg_select, bg_tiles, bg_tilesize>>1);
        PA_LoadRotBgMap(screen, bg_select, bg_map, bg_size);
        PA_InitBg(screen, bg_select, bg_size, wraparound, 1);
        if(dual && !screen){
            PA_SetBgRot(screen, bg_select, 0, 0, 0, -191, 0, 256);
        }
        else{
            PA_SetBgRot(screen, bg_select, 0, 0, 0, 0, 0, 256);
        }
    }
    
    void RotatingBackground::load(){
        if(!loaded){
            loadbackground(screen);
            loaded = true;
        }
    }
    
    void RotatingBackground::loaddual(){
        if(!loaded){
            dual = true;
            loadbackground(1);
            loadbackground(0);
            loaded = true;
        }
    }
    
    void RotatingBackground::remove(){
        if(dual){
            PA_DeleteBg(1, bg_select);
            PA_DeleteBg(0, bg_select);
        }
        else{
            PA_DeleteBg(screen, bg_select);
        }
        loaded = false;
    }
    
    bool RotatingBackground::isloaded(){
        return loaded;
    }
    
    
    void RotatingBackground::setrotcenter(s32 x, s32 y){
        rotcenter_x = x;
        rotcenter_y = y;
        
        if(dual){
            PA_SetBgRot(1, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
            PA_SetBgRot(0, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y-191, angle, zoom);
        }
        else{
            PA_SetBgRot(screen, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
        }
    }
    
    void RotatingBackground::setscroll(s32 x, s32 y){
        scroll_x = x;
        scroll_y = y;
        
        if(dual){
            PA_SetBgRot(1, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
            PA_SetBgRot(0, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y-191, angle, zoom);
        }
        else{
            PA_SetBgRot(screen, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
        }
    }
    
    void RotatingBackground::setangle(s16 angle){
        RotatingBackground::angle = angle;
        
        if(dual){
            PA_SetBgRot(1, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
            PA_SetBgRot(0, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y-191, angle, zoom);
        }
        else{
            PA_SetBgRot(screen, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
        }
    }
    
    void RotatingBackground::setzoom(s32 zoom){
        RotatingBackground::zoom = zoom;
        
        if(dual){
            PA_SetBgRot(1, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
            PA_SetBgRot(0, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y-191, angle, zoom);
        }
        else{
            PA_SetBgRot(screen, bg_select, scroll_x, scroll_y, rotcenter_x, rotcenter_y, angle, zoom);
        }
    }
}
